/*------------------------------------------------------
| noka tab menu v1.0 www.97521.com                     |
| rebin 2014-11-28                                     |
|-----------------------------------------------------*/
var nokatab = Class.create({
	    version: '1.0',
	    initialize : function(cfg) {
	    	this.id=cfg.id; //id
	    	this.cfg = cfg;
	    	this.optab = cfg.optab;//初始化打开
	    	this.tabs = cfg.tabs;//[{id:'',title:'',url:''},{id:'',title:'',url:''}]
	    	this.gridids = cfg.gridids;
	    	this.onselect = cfg.onselect;
	    },
	    show : function(){
	    	var self = this;
	    	self.inittabs();//插入初始化选项卡
	    	self.initevent();
	    },
	    //----------初始化选项卡--------------------------------------------------------------
	    inittabs : function(){
	    	var self = this;
	    	var tabs = self.tabs;
	    	if(undefined !=tabs){
	    		for(var i=0;i<tabs.length;i++){
	    			self.insertTab(tabs[i]);
	    		}
	    	}
	    	self.SelectTabmenu(self.optab);//选种当前选项卡
	    },
	    //--------------选种菜单-----------------------------------------------------------
	    SelectTabmenu : function(menuid){
	    	var self = this;
	    	//----------------设置标题样式--------------------------------------------------
	    	$$('div[id^="'+self.id+'_tdiv_button_"]').each(function(alink,index) {
	    		var mid =self.idtomenuid(alink.id);
	    		if(undefined!=mid && menuid==mid){
	    			alink.removeClassName('notitle_div');
	    			alink.addClassName('title_div');
	    		}else{
	    			alink.removeClassName('title_div');
		    		alink.addClassName('notitle_div');
	    		}
			});
	    	//---------------设置内容区---------------------------------------------------
	    	try{
		    	$$('iframe[id^="'+self.id+'_body_iframe_"]').each(function(alink,index) {
		    		var mid =self.idtomenuid(alink.id);
		    		if(undefined!=mid && menuid==mid){
		    			alink.show();
		    		}else{
		    			alink.hide();
		    		}
				});
	    	}catch(e){}
	    	try{
		    	$$('div[id^="'+self.id+'_body_divhtml_"]').each(function(alink,index) {
		    		var mid =self.idtomenuid(alink.id);
		    		if(undefined!=mid && menuid==mid){
		    			alink.show();
		    		}else{
		    			alink.hide();
		    		}
				});
	    	}catch(e){}
	    	//-------------------------------------------------------------------------
	    	self.rebodySize(menuid);
	    	self.optab = menuid;
	    	//-------------------------------------------------------------------------
	    	try{
		    	if(undefined!=self.onselect){
		    		for(var i=0;i<self.tabs.length;i++){
		    			if(self.tabs[i].id==menuid){
		    				self.onselect(self.tabs[i]);
		    				break;
		    			}
		    		}
		    	}
	    	}catch(e){}
	    	//----------------------------------------------------------------------------
	    	self.redbsize(menuid);
	    },
	    //---------------根据标题id获取选项卡id-----------------------------------------------
	    idtomenuid : function(id){
	    	if(undefined!=id){
	    		try{
	    			return id.substring(id.lastIndexOf('_')+1,id.length);
	    		}catch(e){}
	    	}
	    },
	    redbsize : function(menuid){
	    	var self = this;
	    	setTimeout(function(){
    			try{$(self.id+'_body_iframe_'+menuid).contentWindow.document.fire("widget:noka_grid_reinit", {});}catch(e){}
    		},200);
    		try{
    			$(self.id+'_body_divhtml_'+menuid).fire("widget:noka_grid_resize", {});//触发控件初始事件
    		}catch(e){}
	    },
	    rebodySize : function(menuid){
	    	var self = this;
	    	var csize = $(self.id).getDimensions();
	    	try{
	    		$(self.id+'_body_iframe_'+menuid).setStyle({width:csize.width+'px',height:csize.height+'px'});
	    	}catch(e){}
	    	try{
	    		$(self.id+'_body_divhtml_'+menuid).setStyle({width:csize.width+'px',height:csize.height+'px'});
	    	}catch(e){}
	    },
	    //----------------初始化事件--------------------------------------------------------
	    initevent : function(){
	    	var self = this;
	    	//-----------插入选项卡--------------------------------------------------------
	    	$(self.id).insertTab = function(a){
	    		self.insertTab(a);
	    		self.optab = a.id;
	    		self.SelectTabmenu(self.optab);//选种当前选项卡
	    	};
	    	$(self.id).insertTabs = function(a){
	    		if(undefined!=a){
	    			for(var i=0;i<a.length;i++){
	    				self.insertTab(a[i]);
	    			}
	    		}
	    		self.optab = a[a.length-1].id;
	    		self.SelectTabmenu(self.optab);//选种当前选项卡
	    	};
	    	//------------窗体大小改变--------------------------
	    	Event.observe(window, 'resize', function(){
	    		self.resize();
	    	});
	    	self.resize();
	    	//-----------获取控件id---------------------------
	    	$(self.id).getId = function(){
	    		return self.id;
	    	};
	    },
	    //----------------窗体大小-----------------------------------------------------
	    resize : function(){
	    	var self = this;
	    	self.rebodySize(self.optab);
	    },
	    //------------------插入tab---------------------------------------------------
	    insertTab : function(tab){//{id:'',title:'',url:'',close:true,refresh:true}
	    	var self = this;
	    	var title = $(self.id+'_tab_title_id_div');
	    	var tabBody = $(self.id+'_tdiv_button_'+tab.id);
	    	if(undefined !=tab && undefined == tabBody){
	    		title.insert('<td><div id="'+self.id+'_tdiv_button_'+tab.id+'" class="notitle_div"><span class="title" id="'+self.id+'_title_button_'+tab.id+'">'+tab.title+'</span></div></td>');//标题
	    		self.selectEvent(tab.id);//加载关闭事件
	    	}
	    	if(undefined!=tab){
	    		$(self.id).show();
	    	}
	    },
	    selectEvent : function(menuid){
	    	var self = this;
	    	var tabbutton = $(self.id+'_title_button_'+menuid);
	    	tabbutton.observe('click',function(a){
	    		self.SelectTabmenu(menuid);
	    	});
	    }
 });